package com.xu.cloud.common.limit;

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
 * redis限流注解, 方法上注解优先级高于类上注解
 *
 * @author xuguan
 * @since 2024/1/29
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimit {

    /**
     * default -1 not limit
     */
    int NOT_LIMIT = -1;

    /**
     * default limit prefix
     */
    String RATE_LIMIT_PREFIX = "rate_limit:";

    /**
     * default limit expression
     */
    String CLIENT_IP = "'client_ip'";

    /**
     * limit key expression
     */
    @AliasFor(attribute = "expression")
    String value() default CLIENT_IP;

    /**
     * limit key expression
     */
    @AliasFor(attribute = "value")
    String expression() default CLIENT_IP;

    /**
     * limit key prefix
     */
    String prefix() default RATE_LIMIT_PREFIX;

    /**
     * limit count
     */
    int limitCount() default NOT_LIMIT;

    /**
     * limit duration
     */
    int limitDuration() default NOT_LIMIT;

    /**
     * time unit
     */
    TimeUnit timeUnit() default TimeUnit.MILLISECONDS;

    /**
     * whether limit if request fail
     */
    boolean limitIfReqFail() default true;
}
